/**
 *	流程模块主Flow权限验证
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-22
 */

package com.framework.Flow;


import com.framerock.baselib.Rock;
import com.framework.ACore.Model;
import java.util.ArrayList;
import java.util.Map;

/**
 * 流程模块主Flow权限验证
 * */
public class FlowAuth extends FlowBase {

    /**
     * 查看的类型
     * */
    public static int TYPE_VIEW  = 0;

    /**
     * 添加类型
     * */
    public static int TYPE_ADD  = 1;

    /**
     * 编辑类型
     * */
    public static int TYPE_EDIT  = 2;

    /**
     * 删除类型
     * */
    public static int TYPE_DEL  = 3;

    /**
     * 导入类型
     * */
    public static int TYPE_DAORU  = 4;

    /**
     * 导出类型
     * */
    public static int TYPE_DAOCHU  = 5;



    private Model authModel;
    public FlowAuth(Flow flows) {
        super(flows);
        authModel = Model.get(this.nDb, "flowauth");
    }

    /**
     * 流程单据的权限判断
     * */
    private Boolean isFauth(int lx)
    {
        Boolean bo = null;
        if(mFlow.isflow > 0){
            int zt = mFlow.billStatus();
            if(mFlow.isMydj() && (zt == 0 || zt==2 || zt==5))return true;
            if(mRequest.USERABOOL){
                bo = whereAuth(mRequest.COMPANYID, lx);
                if(bo != null)return bo;
            }
            return false;
        }
        return bo;
    }
    
    /**
     * 是否可以编辑
     * */
    public Boolean isEdit()
    {
        Boolean bo = mFlow.mFlowListener.onisEdit();
        if(bo != null)return bo;
        bo  = isFauth(TYPE_EDIT);
        if(bo != null)return bo;
        if(mFlow.isCompanymode() && mRequest.USEATYPE > 0)return true; //单位管理员可编辑
        if(!mFlow.isCompanymode() && mRequest.USERTYPE > 0)return true;
        if(mFlow.isManage())return true;
        bo = mFlow.whereAuth(FlowWhere.WTYPE_EDIT);
        if(bo != null)return bo;
        if(mRequest.USERABOOL){
            bo = whereAuth(mRequest.COMPANYID, TYPE_EDIT);
            if(bo != null)return bo;
        }
    	return false;
    }
    
    /**
     * 是否可以删除
     * */
    public Boolean isDelete()
    {
        Boolean bo = mFlow.mFlowListener.onisDelete();
        if(bo != null)return bo;
        bo  = isFauth(TYPE_DEL);
        if(bo != null)return bo;
        if(mRequest.USERTYPE==2)return true;
        if(mFlow.isCompanymode() && mRequest.USEATYPE==2)return true; //单位超级管理员可删除普通管理员不可以
        if(mFlow.isManage())return true;
        bo = mFlow.whereAuth(FlowWhere.WTYPE_DEL);
        if(bo != null)return bo;
        if(mRequest.USERABOOL){
            bo = whereAuth(mRequest.COMPANYID, TYPE_DEL);
            if(bo != null)return bo;
        }
    	return false;
    }
    
    /**
     * 是否可以查看详情页
     * */
    public Boolean isView()
    {
        Boolean bo = mFlow.mFlowListener.onisView();
        if(bo != null)return bo;
        if(mFlow.isMydj())return true; //自己单据
        if(mRequest.USERTYPE > 0 || mFlow.isCompanymode())return true;
        if(mFlow.isManage())return true;
        bo = mFlow.whereAuth(FlowWhere.WTYPE_VIEW);
        if(bo != null)return bo;
        if(mRequest.USERABOOL){
            bo = whereAuth(mRequest.COMPANYID, TYPE_VIEW);
            if(bo != null)return bo;
        }
    	return false;
    }

    /**
     * 是否可以新增
     * */
    public Boolean isAdd()
    {
        Boolean bo = mFlow.mFlowListener.onisAdd();
        if(bo != null)return bo;
        if(mFlow.isCompanymode() && mRequest.USEATYPE > 0)return true; //单位管理员可编辑
        if(!mFlow.isCompanymode() && mRequest.USERTYPE > 0)return true;
        if(mFlow.isManage())return true;
        //if(mFlow.UnitModeBo)return true; //单位模块任何人都可添加
        if(mRequest.USERABOOL){
            bo = whereAuth(mRequest.COMPANYID, TYPE_ADD);
            if(bo != null)return bo;
        }
        return false;
    }

    /**
     * 读取条件的判断
     * */
    public Boolean whereAuth(String cid, int type)
    {
        Map<String, String> da = mRequest.getUserainfo(),rs;
        String where = "`cid`="+cid+" AND `modeid`="+mFlow.modeid+" AND `type`="+type+" AND `status`=1";
        where += " AND "+mFlow.receidWhere(da, "`receid`", "");
        ArrayList<Map<String, String>> rows = authModel.getAll(where);
        int to,i,co;
        if(rows != null){
            to = rows.size();
            if((type == TYPE_ADD || type == TYPE_DAORU || type==TYPE_DAOCHU) && to>0)return true;
            for(i=0;i<to;i++){
                rs      = rows.get(i);
                where   = mFlow.getFlowwheres(rs.get("where"));
                if(Rock.isEmpt(where))return true;
                co      = mFlow.mModel.getCount("`id`="+mFlow.mid+" AND "+where+"");
                if(co>0)return  true;
            }
        }
        return null;
    }

    /**
     * 是否可以导出
     * */
    public Boolean isDaochu()
    {
        Boolean bo;
        if(mRequest.USERABOOL){
            bo = whereAuth(mRequest.COMPANYID, TYPE_DAOCHU);
            if(bo != null)return bo;
        }
        return false;
    }

    /**
     * 是否可以导入
     * */
    public Boolean isDaoru()
    {
        Boolean bo;
        if(mRequest.USERABOOL){
            bo = whereAuth(mRequest.COMPANYID, TYPE_DAORU);
            if(bo != null)return bo;
        }
        return false;
    }
}
